<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/5/12
 * Time: 13:30
 */
header("Content-type: text/html; charset=utf-8");
include 'helper.php';
include 'MySqlConnect.php';
//$token_code = $_GET['code'];
//$id = $_GET['id'];
//$state = $_GET['state'];
//exit;
//echo "code=".$APPID."<br>".$APPSecret."<br>".$token_code; exit;
//通过code获取到openid

//$code_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $APPID . "&secret=" . $APPSecret . "&code=" . $token_code . "&grant_type=authorization_code";
//$code_result = curl($code_url);
//$code_data = json_decode($code_result, true);
//$openId = $code_data['openid'];

$token_code = $_GET['code'];
$id = $_GET['id'];
$state = $_GET['state'];
//exit;
//echo "code=".$APPID."<br>".$APPSecret."<br>".$token_code; exit;
//通过code获取到openid

$code_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $APPID . "&secret=" . $APPSecret . "&code=" . $token_code . "&grant_type=authorization_code";
$code_result = curl($code_url);
$code_data = json_decode($code_result, true);
$openId = $code_data['openid'];

$mysqlconnect = new MySqlConnect();
$querystr = "select * from user where openid='$openId'";
$info =json_decode($mysqlconnect->GetDataSetJsonPHP($querystr)) ;
$count = count($info);
if($count=="0")
{
	$nowtime = date('Y-m-d H:i:s');
	$jieguo = $mysqlconnect->ExecuteData("insert into user values(null,'$openId','','','$nowtime','0','0','0','0','0','0')");
}

setcookie("openid",$openId);
echo "<script language='javascript' type='text/javascript'>";  
echo "window.location.href='index.html'";  
echo "</script>"; 

?>